import { PDADTO } from './../../../shared/service-proxy/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Component, OnInit, EventEmitter, Output, Injector } from '@angular/core';
import { ServiceProxy, ElectronicLockDTO, FixedReadingDeviceDTO } from '@shared/service-proxy/service-proxies';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-set-device-reason-modal',
  templateUrl: './set-device-reason-modal.component.html',
  styleUrls: ['./set-device-reason-modal.component.less']
})
export class SetDeviceReasonModalComponent extends AppComponentBase implements OnInit {
  @Output() modalSave = new EventEmitter<any>();
  isVisible = false;
  isOkLoading = false;
  model: any;
  type: string;
  title: string;
  validateForm: FormGroup;
  constructor(injector: Injector, private _service: ServiceProxy, private fb: FormBuilder) {
    super(injector)
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      reason: [null, [Validators.nullValidator, Validators.maxLength(255)]]
    });
    this.model = { reason: "", id: "", enableFlag: false };
  }

  handleCancel() {
    this.isVisible = false;
  }

  handleOk() {
    this.isOkLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    if (this.validateForm.valid) {

      if (this.type == "1") {
        const electronicLock = new ElectronicLockDTO();
        electronicLock.id = this.model.id,
          electronicLock.enableFlag = this.model.enableFlag;
        electronicLock.reason = this.model.reason;
        this._service.updateElectronicStatusUsingPUT(electronicLock.id, electronicLock).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.message.success(this.l("save_success"));
            this.modalSave.emit();
            this.isVisible = false;
          }
          else {
            this.message.info(this.l(result.error.message));
          }
        }, error => {
          this.isOkLoading = false;
          this.message.error(error.message);
        });
      }
      else if (this.type == "2") {
        const device = new FixedReadingDeviceDTO();
        device.id = this.model.id;
        device.reason = this.model.reason;
        device.enableFlag = this.model.enableFlag;
        this._service.updateDeviceStatusUsingPUT(device.id, device).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.message.success(this.l("save_success"));
            this.modalSave.emit();
            this.isVisible = false;
          }
          else {
            this.message.info(this.l(result.error.message));
          }

        }, error => {
          this.isOkLoading = false;
          this.message.error(error.message);
        });
      } else if (this.type == "3") {
        const pda = new PDADTO();
        pda.id = this.model.id;
        pda.reason = this.model.reason;
        pda.enableFlag = this.model.enableFlag;
        this._service.updatePdaStatusUsingPUT(pda.id, pda).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.message.success(this.l("save_success"));
            this.modalSave.emit();
            this.isVisible = false;
          }
          else {
            this.message.info(this.l(result.error.message));
          }

        }, error => {
          this.isOkLoading = false;
          this.message.error(error.message);
        });
      }
    }
    else {
      this.isOkLoading = false;
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
    }
  }

  show(id: string, enableFlag: boolean, type: string) {
    this.isVisible = true;
    this.model.id = id;
    this.model.enableFlag = !enableFlag;
    this.type = type;
    if (this.model.enableFlag)
      this.title = this.l("enable");
    else
      this.title = this.l("disenable");
    this.model.reason = "";
  }
}

